import {ref, createVNode} from "vue";
import axios from "@/utils/axios";
import {message, Modal} from "ant-design-vue";
import EveryPageEnum from "@/enums/everyPageEnum"
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

class DeviceQueryInfo{
    sectionId: number | undefined = undefined
    kind: Array<number> = new Array<number>()
}

class DeviceFormState{
    sections: Array<DeviceQueryInfo> = [new DeviceQueryInfo()]
    status: number | undefined = 0
    deviceName: string | undefined = undefined
    deviceSn: string | undefined = undefined
    checkStatus: number | undefined = undefined
    startPage: number | undefined = 1
    everyPage: number | undefined = EveryPageEnum.small
}

class DeviceBaseInfo{
    deviceId:number = -1
    deviceName:string|undefined = ""
    deviceSn: number|undefined
    status:number = 0
    deviceKind:number|undefined
    deviceColor:string = "#000000"
    sectionId:number|undefined
}

export default ()=>{
    // 表单对象
    const deviceRef = ref()
    // 表单内容
    const deviceFormState = ref(new DeviceFormState())
    // 当前已查询条件
    const deviceFormState2 = ref(new DeviceFormState())
    // 设备列表
    const deviceList:any = ref({})

    /**
     * 重置表单
     */
    const resetForm = ()=>{
        deviceFormState.value = new DeviceFormState()
    }

    /**
     * 获取设备列表
     */
    const getDeviceList = ()=>{
        if(!deviceFormState.value.sections[0].sectionId) {
            message.info("请选择归属标段")
            return
        }
        if(typeof deviceFormState.value.deviceName != 'undefined')
            deviceFormState.value.deviceName =
                (deviceFormState.value.deviceName as string).replace('/\\s+/g','')===""?undefined:deviceFormState.value.deviceName
        if(typeof deviceFormState.value.deviceSn != 'undefined')
            deviceFormState.value.deviceSn =
                (deviceFormState.value.deviceSn as string).replace('/\\s+/g','')===""?undefined:deviceFormState.value.deviceSn

        axios.post("/common/findDevice",
            deviceFormState.value).then((res:any)=>{
            if(res.success){
                deviceFormState2.value = deviceFormState.value
                deviceList.value = res.data
            }else{
                message.info(res.msg)
            }
        })
    }

    /**
     * 跳转下一页
     * todo 这边的查询都写乱了,有时间重新整理一下
     */
    const nextDeviceList = (pageInfo:any)=>{
        deviceFormState2.value.startPage = pageInfo.current
        axios.post("/common/findDevice",
            deviceFormState2.value).then((res:any)=>{
            if(res.success){
                deviceList.value = res.data
            }else{
                message.info(res.msg)
            }
        })
    }

    // 表单对象
    const deviceBaseFormRef = ref()
    // 表单模态框
    const showDeviceBaseModal = ref(false)
    // 表单信息
    const deviceBaseForm = ref(new DeviceBaseInfo())
    // 表单验证规则
    const driverBaseRules = {
        deviceName:[{
            required: true,
            message: "设备名称不能为空",
            // message: '设备名称请在1-15字之间',
            type: 'string',
            trigger: 'blur'
        }],
        deviceKind:[{
            required: true,
            message: '请选择机械类型',
            type: 'any',
            trigger: 'change'
        }],
        sectionId:[{
            required: true,
            message: '请选择归属标段',
            type: 'any',
            trigger: 'change'
        }],
        // deviceSn:[{
        //     required: true,
        //     message: 'SN必须为6位整数',
        //     validator:(rule:any,value:any,callback:any)=>{
        //         const number = Number(value)
        //         if (!Number.isInteger(number)||(number<100000 || number>999999)) {
        //             return Promise.reject('请输入大于等于0的整数')
        //         }else {
        //             return Promise.resolve()
        //         }
        //     },
        //     trigger: 'blur'
        // }]
    }
    /**
     * 新增||更新设备
     */
    const setDeviceBaseInfo = ()=>{
        deviceBaseFormRef.value.validate().then(()=>{
            deviceBaseForm.value.deviceSn = Number(deviceBaseForm.value.deviceSn)
            axios.post("/common/updateDeviceInfoByDeviceId",
                deviceBaseForm.value).then((res:any)=>{
                if(res.success){
                    if (deviceBaseForm.value.deviceId>0){
                        let inDevice = deviceList.value.deviceInfo[deviceList.value.deviceInfo.findIndex((item:any)=>{
                            return item.deviceId === deviceBaseForm.value.deviceId
                        })]
                        inDevice.deviceName = deviceBaseForm.value.deviceName
                        inDevice.deviceColor = deviceBaseForm.value.deviceColor
                    }
                    message.info("保存成功")
                    showDeviceBaseModal.value = false
                }else{
                    message.info(res.msg)
                }
            })
        }).catch((e:any)=>{
            console.log(e)
        })
    }
    /**
     * 设备退场
     * @param deviceId 设备id
     * @param callback 回调函数
     */
    const walkOff = (deviceId:number,callback:Function|null=null)=>{
        axios.post("/board/push/commitOutDevice", {deviceId: deviceId, kind: 0}).then((res:any)=>{
            if(res.success){
                message.info("退场审核提交成功")
                showDeviceBaseModal.value = false
                if(callback) callback()
            }else{
                message.info(res.msg)
            }
        })
    }
    /**
     * 设备退场 模态框
     * @param deviceInfo 设备信息
     * @param callback 回调函数
     */
    const openWalkOff = (deviceInfo:any)=>{
        if (deviceInfo.status == 2 || deviceInfo.status == 6) {
            Modal.confirm({
                title: `是否提交名称为${deviceInfo.deviceName},SN为${deviceInfo.deviceSn}的设备的退场申请?`,
                icon: createVNode(ExclamationCircleOutlined),
                okText: '确定',
                okType: 'danger',
                cancelText: '取消',
                onOk() {
                    walkOff(deviceInfo.deviceId,()=>{
                        // todo 退场审核状态 == 退场审核中
                        deviceInfo.outStatus = 1
                        // deviceList.value.deviceInfo.splice(deviceList.value.deviceInfo.indexOf(deviceInfo),1)
                    });
                }
            });
        } else if (deviceInfo.status == 0 || deviceInfo.status == 1 || deviceInfo.status == 3) {
            message.info("该机械未入场")
        } else if (deviceInfo.status == 4) {
            message.info("已提起退场申请")
        } else if (deviceInfo.status == 5) {
            message.info("已退场，请勿重复提交")
        }
    }
    /**
     * 打开编辑基本信息弹窗
     * @param oldInfo 旧设备信息.1.若无,则为新增;2.若有,则为编辑
     */
    const openDeviceBaseInfoModal = (oldInfo: any)=>{
        deviceBaseFormRef.value.resetFields()
        let deviceBaseForm2 = new DeviceBaseInfo()
        if(oldInfo){
            deviceBaseForm2.deviceId = oldInfo.deviceId
            deviceBaseForm2.deviceName = oldInfo.deviceName
            deviceBaseForm2.deviceSn = Number(oldInfo.deviceSn)
            deviceBaseForm2.status = oldInfo.deviceStatus
            deviceBaseForm2.deviceKind = oldInfo.deviceKind
            deviceBaseForm2.deviceColor = oldInfo.deviceColor
            deviceBaseForm2.sectionId = oldInfo.idSection
        }
        Object.assign(deviceBaseForm.value, deviceBaseForm2)

        showDeviceBaseModal.value = true
    }


    return{
        deviceRef,
        deviceFormState,
        deviceList,
        resetForm,
        getDeviceList,
        nextDeviceList,

        deviceBaseFormRef,
        showDeviceBaseModal,
        deviceBaseForm,
        driverBaseRules,
        openWalkOff,
        setDeviceBaseInfo,
        openDeviceBaseInfoModal
    }
}
